<?php
/* ******************************
 *	Transfer aJax actions
 * ******************************
 */
 
include('../../includes/classes/core.php');
include('../../includes/classes/logger.class.php');

include('../../includes/classes/log.class.php');


$log = new log();

$action	= $_REQUEST['act'];
$user_id	= $_COOKIE['USERID'];
$error	= '';
$data	= '';

//$_log   = Logger::instance('../../log/operations/transfer/', Logger::OFF);

switch ($action) {
    case 'get_add_page':
		$error = "იყოს მაინც";
		
        break;
    case 'get_edit_page':
    	$CheckId   			= $_REQUEST['id'];
    	$page				= GetPage($CheckId);
    	$data				= array('page'	=> $page);	    
        
        break;
    case 'submit_check':
    	$user_id           = $_COOKIE['USERID'];
    	$CheckId           = $_REQUEST['check_id'];
    	$comment           = $_REQUEST['comment'];
    	
    	UpdateCheck($user_id, $CheckId, $comment);
    	
    	break;
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    
	    $rResult = mysql_query("SELECT  pos_check.id,
	    								pos_check.check_date,
	    								object.`name`,
	    								pe3.`name` AS `last_pos_name`,
							            pe1.`name`,
							            pos_check.money_in_pos,
							            pos_check.real_money,
	    								pos_check.real_money - pos_check.money_in_pos,
							            pe2.`name`,
							            pos_check.submit_date,
	    								pos_check.comment,
	    								pos_check.manager_comment,
							            IF(pos_check.`status` = 0, '<span class=\"not_submited\"> დასადასტურებელი</span>', 'დადასტურებული') AS `status`								    
							FROM      `pos_check`
							LEFT JOIN  users  AS us1 ON pos_check.pos_user_id = us1.id
							LEFT JOIN  persons AS pe1 ON pe1.id = us1.person_id
							LEFT JOIN  users AS us2 ON pos_check.manager_id = us2.id
							LEFT JOIN  persons AS pe2 ON pe2.id = us2.person_id
	    		  			LEFT JOIN  users   AS us3 ON pos_check.last_pos_user_id = us3.id
							LEFT JOIN  persons AS pe3 ON pe3.id = us3.person_id
	    					LEFT JOIN  object ON pos_check.pos_id = object.id
							ORDER BY  pos_check.check_date DESC");
		
		$data = array(
			"aaData"	=> array()
		);
		
		if(!$rResult){
			$error = 'Invalid query: ' . mysql_error();
		}else{
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];			
				}
				$data['aaData'][] = $row;
			}
			
			//$_log->logInfo('get_list');
		}
		
        break;
		
        break;   
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Transfer Functions
 * ******************************
 */


function GetPage($CheckId){
	
  $res = mysql_fetch_assoc(mysql_query("SELECT    `pos_check`.`status` AS `status`
											FROM  `pos_check`
											WHERE `pos_check`.`id` = $CheckId"));
  
  $ch = $res['status'];
	
  $res = mysql_fetch_assoc(mysql_query("SELECT  pos_check.id AS `id`,
							            pe1.`name` AS `name`,
  										pe3.`name` AS `last_pos_name`,
										object.`name`  AS `object`,
							            pos_check.check_date AS `check_date`,
							            pos_check.money_in_pos AS `money`,
							            pos_check.real_money AS `real_money`,
  										pos_check.real_money - pos_check.money_in_pos AS `difference`,
							            pe2.`name` AS `manager_name`,
							            pos_check.submit_date AS `submit_date`,
	    								pos_check.comment AS `comment`,
										pos_check.manager_comment AS `manager_comment`,
							            IF(pos_check.`status` = 0, 'დასადასტურებელი', 'დადასტურებული') AS `status`
							FROM      `pos_check`
							LEFT JOIN  users  AS us1 ON pos_check.pos_user_id = us1.id
							LEFT JOIN  persons AS pe1 ON pe1.id = us1.person_id					
							LEFT JOIN  users AS us2 ON pos_check.manager_id = us2.id
							LEFT JOIN  persons AS pe2 ON pe2.id = us2.person_id
  							LEFT JOIN  users   AS us3 ON pos_check.last_pos_user_id = us3.id
							LEFT JOIN  persons AS pe3 ON pe3.id = us3.person_id	
							LEFT JOIN  object ON pos_check.pos_id = object.id
							WHERE pos_check.id = $CheckId"));
	
	$data = '<div id="dialog-form">
				<fieldset>
					<legend>გასწორება</legend>
		
			    	<table class="dialog-form-table">
						<tr>
							<td style="width: 170px;"><label for="op_date">შემოწმების თარიღი:</label></td>
							<td>
								<span id="op_date" style="font-weight:bold; font-size:15px;">'.$res['check_date'].'</span>
							</td>
					 	</tr>
						<tr>
							<td style="width: 170px;"><label for="op_date">დადასტურების თარიღი:</label></td>
							<td>
								<span id="op_date" style="font-weight:bold; font-size:15px;">'.$res['submit_date'].'</span>
							</td>
					 	</tr>					
		       <table style="margin-bottom:20px; width:450px;">
		
			       <tr class="restore_list_header" style="background:none !important;">  
						  <td class="restore_list_header" style="background:none !important; color:gray;">ობიექტი</td>	  
						  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['object'].'</td>
					  </tr>	
					  <tr class="restore_list_header" style="background:none !important;">  
						  <td class="restore_list_header" style="background:none !important; color:gray;">ჩააბარა</td>	  
						  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['last_pos_name'].'</td>
					  </tr>						  		
					  <tr class="restore_list_header" style="background:none !important;">  
						  <td class="restore_list_header" style="background:none !important; color:gray;">ჩაიბარა</td>	  
						  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['name'].'</td>
					  </tr>	
					  <tr class="restore_list_header" style="background:none !important;">  
						  <td class="restore_list_header" style="background:none !important; color:gray;">თანხა სალაროში</td>	  
						  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['money'].'</td>
					  </tr>						  		
					  <tr class="restore_list_header" style="background:none !important;">  
						  <td class="restore_list_header" style="background:none !important; color:gray;">რეალური თანხა სალაროში</td>	  
						  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['real_money'].'</td>
					  </tr>
					  <tr class="restore_list_header" style="background:none !important;">  
						  <td class="restore_list_header" style="background:none !important; color:gray;">სხვაობა</td>	  
						  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['difference'].'</td>
					  </tr>
					  <tr class="restore_list_header" style="background:none !important;">  
						  <td class="restore_list_header" style="background:none !important; color:gray;">სალაროს გამსწორებელი</td>	  
						  <td class="restore_list_header" style="background:none !important; color:tomato; font-size:15px;">'.$res['manager'].'</td>
					  </tr>
					  <tr>
						  <td colspan="2" style="text-align:center; padding:15px 0 5px 0;">
						  		მოლარის შენიშვნა
						  </td>		 
					  </tr> 						  			
					  <tr>
						  <td colspan="2">
						  		<textarea rows="4" cols="61" style="resize: none;" disabled>'.$res['comment'].'</textarea>
						  </td>		 
					  </tr> 
					   <tr>
						  <td colspan="2" style="text-align:center; padding:15px 0 5px 0;">
						  		დამდასტურებლის  შენიშვნა
						  </td>		 
					  </tr> 						  			
					  <tr>
						  <td colspan="2" >
						  		<textarea id="manager_comment" rows="4" cols="61" style="resize: none;">'.$res['manager_comment'].'</textarea>
						  </td>		 
					  </tr> 				
					</table>	
						
					</table>
					<!-- ID -->
					<input type="hidden" id="check_id" value="'.$CheckId.'" />
		        </fieldset>
    	</div>';
	return $data;
}

function UpdateCheck($user_id, $CheckId, $comment){
	
	GLOBAL $log;
	
	$date = date('Y-m-d H:i:s');
	
	$log->setUpdateLogBefore('pos_check', $CheckId);
	
	mysql_query("UPDATE pos_check
				SET
				`pos_check`.`manager_id` = $user_id,
				`pos_check`.`submit_date` = '$date',
				`pos_check`.`manager_comment` = '$comment',
				`pos_check`.`status` = 1
				WHERE pos_check.id = $CheckId");
	
	$log->setUpdateLogAfter('pos_check', $CheckId);
	
}
    

?>